
<!DOCTYPE html>
<html>

	<head>
	<title>CAIJL's Blog</title>
	<meta charset="utf-8"/>
	<link rel="stylesheet" href="/font-awesome-4.7.0/css/font-awesome.min.css">

	<link rel="stylesheet" href="/bootstrap-3.3.7/css/bootstrap.min.css">
	<script src="/jquery-2.1.1/jquery.min.js"></script>
	<script src="/bootstrap-3.3.7/js/bootstrap.min.js"></script>

	<link rel="stylesheet" href="/css/bootstrap-slider.min.css">
	<script src="/js/bootstrap-slider.min.js"></script>
	
	<link rel="stylesheet" href="/css/main.css">
	<script src="/js/main.js"></script>
	
	<link rel="stylesheet" href="/css/cursor.css">
	<script src="/js/cursor.js"></script>
	
	
	<link rel="stylesheet" href="/css/code.css">
	<!--link rel="stylesheet" href="/css/markdown.css"-->
	
	<link rel="stylesheet" href="/css/head.css">

	<script src="/js/window.js"></script>
	<script src='//unpkg.com/valine/dist/Valine.min.js'></script>
	</head>
<body>

	<link rel="stylesheet" href="/katex/katex.min.css" crossorigin="anonymous">
	<script src="/katex/katex.min.js" crossorigin="anonymous"></script>
	<script src="/katex/contrib/mathtex-script-type.min.js" defer></script>
	<script defer src="/katex/contrib/auto-render.min.js"crossorigin="anonymous"
	    onload="renderMathInElement(document.body);"></script>
	<script>document.addEventListener("DOMContentLoaded", function() {renderMathInElement(document.body,{"delimiters":[{left: "$", right: "$", display: false}]});});</script>
	<nav class="navbar navbar-default header card" role="navigation" style="width: 100%;">
	<div class="container-fluid"> 
	<div class="navbar-header">
		<button type="button" class="navbar-toggle" data-toggle="collapse"
				data-target="#example-navbar-collapse">
			<span class="icon-bar"></span>
			<span class="icon-bar"></span>
			<span class="icon-bar"></span>
		</button>
		<a class="navbar-brand" href="/"><span><span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi mathvariant="bold">C</mi><mi mathvariant="bold">A</mi><mi mathvariant="bold">I</mi><mi mathvariant="bold">J</mi><msup><mi mathvariant="bold">L</mi><mo mathvariant="bold" lspace="0em" rspace="0em">′</mo></msup><mi mathvariant="bold">s</mi><mi mathvariant="bold">B</mi><mi mathvariant="bold">L</mi><mi mathvariant="bold">O</mi><mi mathvariant="bold">G</mi></mrow><annotation encoding="application/x-tex">\mathbf{CAIJL'sBLOG}</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height: 0.751892em; vertical-align: 0em;"></span><span class="mord"><span class="mord mathbf">C</span><span class="mord mathbf">A</span><span class="mord mathbf">I</span><span class="mord mathbf">J</span><span class="mord"><span class="mord mathbf">L</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span class="vlist" style="height: 0.751892em;"><span class="" style="top: -3.063em; margin-right: 0.05em;"><span class="pstrut" style="height: 2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mathbf mtight">′</span></span></span></span></span></span></span></span></span><span class="mord mathbf">s</span><span class="mord mathbf">B</span><span class="mord mathbf">L</span><span class="mord mathbf">O</span><span class="mord mathbf">G</span></span></span></span></span></span></a>
	</div>
	<div class="collapse navbar-collapse" id="example-navbar-collapse">
		<ul class="nav navbar-nav" style="background-color:#fcfcfc">
			<li><a href="/"><i class="menu-item-icon fa fa-fw fa-home"></i>首页</a></li>
			<li><a href="/archives/"><i class="menu-item-icon fa fa-fw fa-archive"></i>文章</a></li>
			<li><a href="/tags/"><i class="menu-item-icon fa fa-fw fa-tags"></i>标签</a></li>
			<li><a href="/settings/"><i class="menu-item-icon fa fa-fw fa-cogs"></i>设置</a></li>
			<!--li><a href="/about/"><i class="menu-item-icon fa fa-fw fa-user"></i>关于</a></li-->
			<li><a href="/games/"><i class="menu-item-icon fa fa-fw fa-gamepad"></i>其它</a></li>
			

		</ul>
	</div>
	</div>
	</nav>
<div class="container">
	<div style="height: 60px;"></div>
	<div class="row" >
		<div class="col-md-8">
			<div class="panel panel-default card" style="padding: 10px;position: relative;">
                <img src='/img/bg/22.png' style="width:100%"/>
				<span style="position: absolute;left:15px;bottom:15px;width:90%;"><font class="view-text" style="color:#fcfcfc;font-size:25px">题解 P6887 【[CEOI2006]Queue】</font><br><a href="/tags/2020/" class="tag"><span  style="background-color: rgb(52, 152, 219);">2020</span></a>&nbsp;<a href="/tags/线段树/" class="tag"><span  style="background-color: rgb(231, 76, 60);">线段树</span></a>&nbsp;<a href="/tags/链表/" class="tag"><span  style="background-color: rgb(231, 76, 60);">链表</span></a>&nbsp;<a href="/tags/题解/" class="tag"><span  style="background-color: rgb(82, 196, 26);">题解</span></a></span>
			</div>
			<div class="panel panel-default card" style="padding: 10px;" id="main">
                <h2 id="_1">题意</h2>
<p>一开始将一列人按编号从<script type="math/tex">1</script>开始顺序排列，然后进行<script type="math/tex">A</script>次操作，每次将编号为<script type="math/tex">A</script>的人拿出，放在编号为 <script type="math/tex">B</script> 的人前面，所有操作进行完后有<script type="math/tex">Q</script>个问题，询问编号为<script type="math/tex">X</script>的人的位置，或者询问在<script type="math/tex">X</script>号位置上的人的编号。</p>
<p>
<script type="math/tex">2≤N≤50000,1≤A,B≤10^9</script>
</p>
<p>
<script type="math/tex">1≤Q≤50000,1≤X≤10^9</script>
</p>
<!--more-->
<h2 id="_2">题解</h2>
<p>定位：<script type="math/tex">\rm ds</script>简单题 <del>假装我没有调两天才调出来</del></p>
<p>首先我们不去考虑数据范围，假设<script type="math/tex">\max{a_i,b_i}\le 1000000</script>，那么该怎么做呢？</p>
<p>观察操作：拿出一个数，在插到另一个数前面，可以用双向链表轻松解决。</p>
<p>为<script type="math/tex">x</script>记录其前驱<script type="math/tex">pre</script>与后继<script type="math/tex">nxt</script>，那么要求删除可以看做把他的前驱与后继连起来，而插入就相当于在一个数和他的前驱之间加上一个数。十分简单，这里不过多赘述。</p>
<p>最后只要找到前驱为<script type="math/tex">0</script>的数再不断往后找就可以复原这个序列了。</p>
<p><del>然而CEOI没有这个部分分差评</del></p>
<p>再考虑上述算法为什么慢¿显然对于<script type="math/tex">1\to 1000000000</script>大部分数字的前驱与后继并没有改变，仍然是<script type="math/tex">x-1</script>与<script type="math/tex">x+1</script>，因此我们把所有的数分成两类：
1. 前驱或后继更改过的
1. 前驱与后继不变的</p>
<p>显然，第<script type="math/tex">1</script>类的个数不会超过<script type="math/tex">3n</script>。那么我们可以用<script type="math/tex">\rm map</script>来维护第<script type="math/tex">1</script>类值的前驱与后继，再把所有前驱或后继更改过的数字拿出来变成若干块。</p>
<p><img alt="" src="/img/ppsme0d8.png"></p>
<p>显然，只有红框里的数前驱与后继发生了变化。我们需要一种东西来找到并记录红框。</p>
<p>如果我们用<script type="math/tex">\rm map</script>来维护每一个值的前驱与后继，那么对于每一个块的开始，其前驱必然不在<script type="math/tex">\rm map</script>中出现，我们可以利用这点性质找到每一个起始位置，并把每一段塞进<script type="math/tex">\rm vector</script>里。</p>
<p>然后对于剩下的数，其必然是递增的，且可以看做是<script type="math/tex">[1,100000000]</script>删去第<script type="math/tex">1</script>类数。</p>
<p>由于希望这些块按前后排序，我们对每个块记录整个块的前驱与后继，那么按前驱排序就把这些块前后排序了。</p>
<p>那么对于每个块，我们可以容易地维护他前面的数字总数。两个块之间的数字数为<script type="math/tex">pre_i-nxt_{i-1}+1</script>。那么对于块内的数，其位置与对相应位置的值也呼之欲出了。</p>
<p>接下来就是不在块内的数：</p>
<h3 id="_3">给位置求编号</h3>
<p>我们记录所有块结束时的数字总数，那么肯定能找到第一个块，使得<script type="math/tex">sum_i\le x</script>，二分可以轻松解决。</p>
<p>那么<script type="math/tex">x</script>位置上的值，就是第 <script type="math/tex">x-\text{再此之前的1类个数}</script> 个第<script type="math/tex">2</script>类值。在此之前的<script type="math/tex">1</script>类个数可以前缀和维护。</p>
<h3 id="_4">给编号求位置</h3>
<p>找到第一个块使得<script type="math/tex">nxt_i\le x</script>，二分一下就好了。</p>
<p>然后<script type="math/tex">x</script>的位置，就是<script type="math/tex">x\text{在第二类中的编号}+\text{再此之前的1类个数}</script>。维护方式类似。</p>
<p>于是最后只剩下一个问题：第<script type="math/tex">2</script>类数最多有<script type="math/tex">1000000000</script>个，如何给数字求编号或给编号求数字。事实上，动态开点线段树可以解决。每个节点记录一个已经删去了多少个，再乱搞一下就好了。</p>
<h2 id="_5">代码</h2>
<p>边界需要注意一下，其他也没什么了。
<div class="highlight"><pre><span></span><code><span class="linenos" data-linenos="  1 "></span><span class="cp">#include</span><span class="cpf">&lt;bits/stdc++.h&gt;</span><span class="cp"></span>
<span class="linenos" data-linenos="  2 "></span><span class="k">namespace</span> <span class="n">in</span><span class="p">{</span>
<span class="linenos" data-linenos="  3 "></span>    <span class="kt">char</span> <span class="n">buf</span><span class="p">[</span><span class="mi">1</span><span class="o">&lt;&lt;</span><span class="mi">21</span><span class="p">],</span><span class="o">*</span><span class="n">p1</span><span class="o">=</span><span class="n">buf</span><span class="p">,</span><span class="o">*</span><span class="n">p2</span><span class="o">=</span><span class="n">buf</span><span class="p">;</span>
<span class="linenos" data-linenos="  4 "></span>    <span class="kr">inline</span> <span class="kt">int</span> <span class="n">getc</span><span class="p">(){</span><span class="k">return</span> <span class="n">p1</span><span class="o">==</span><span class="n">p2</span><span class="o">&amp;&amp;</span><span class="p">(</span><span class="n">p2</span><span class="o">=</span><span class="p">(</span><span class="n">p1</span><span class="o">=</span><span class="n">buf</span><span class="p">)</span><span class="o">+</span><span class="n">fread</span><span class="p">(</span><span class="n">buf</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="o">&lt;&lt;</span><span class="mi">21</span><span class="p">,</span><span class="n">stdin</span><span class="p">),</span><span class="n">p1</span><span class="o">==</span><span class="n">p2</span><span class="p">)</span><span class="o">?</span><span class="nl">EOF</span><span class="p">:</span><span class="o">*</span><span class="n">p1</span><span class="o">++</span><span class="p">;}</span>
<span class="linenos" data-linenos="  5 "></span>    <span class="k">template</span> <span class="o">&lt;</span><span class="k">typename</span> <span class="nc">T</span><span class="o">&gt;</span><span class="kr">inline</span> <span class="kt">void</span> <span class="n">read</span><span class="p">(</span><span class="n">T</span><span class="o">&amp;</span> <span class="n">t</span><span class="p">){</span>
<span class="linenos" data-linenos="  6 "></span>        <span class="n">t</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="kt">int</span> <span class="n">f</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="kt">char</span> <span class="n">ch</span><span class="o">=</span><span class="n">getc</span><span class="p">();</span><span class="k">while</span> <span class="p">(</span><span class="o">!</span><span class="n">isdigit</span><span class="p">(</span><span class="n">ch</span><span class="p">)){</span><span class="k">if</span><span class="p">(</span><span class="n">ch</span><span class="o">==</span><span class="sc">&#39;-&#39;</span><span class="p">)</span><span class="n">f</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span><span class="n">ch</span><span class="o">=</span><span class="n">getc</span><span class="p">();}</span>
<span class="linenos" data-linenos="  7 "></span>        <span class="k">while</span><span class="p">(</span><span class="n">isdigit</span><span class="p">(</span><span class="n">ch</span><span class="p">)){</span><span class="n">t</span><span class="o">=</span><span class="n">t</span><span class="o">*</span><span class="mi">10</span><span class="o">+</span><span class="n">ch</span><span class="mi">-48</span><span class="p">;</span><span class="n">ch</span> <span class="o">=</span> <span class="n">getc</span><span class="p">();}</span><span class="k">if</span><span class="p">(</span><span class="n">f</span><span class="p">)</span><span class="n">t</span><span class="o">=-</span><span class="n">t</span><span class="p">;</span>
<span class="linenos" data-linenos="  8 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="  9 "></span>    <span class="k">template</span> <span class="o">&lt;</span><span class="k">typename</span> <span class="nc">T</span><span class="p">,</span><span class="k">typename</span><span class="p">...</span> <span class="n">Args</span><span class="o">&gt;</span> <span class="kr">inline</span> <span class="kt">void</span> <span class="n">read</span><span class="p">(</span><span class="n">T</span><span class="o">&amp;</span> <span class="n">t</span><span class="p">,</span> <span class="n">Args</span><span class="o">&amp;</span><span class="p">...</span> <span class="n">args</span><span class="p">){</span><span class="n">read</span><span class="p">(</span><span class="n">t</span><span class="p">);</span><span class="n">read</span><span class="p">(</span><span class="n">args</span><span class="p">...);}</span>
<span class="linenos" data-linenos=" 10 "></span><span class="p">}</span>
<span class="linenos" data-linenos=" 11 "></span><span class="k">namespace</span> <span class="n">out</span><span class="p">{</span>
<span class="linenos" data-linenos=" 12 "></span>    <span class="kt">char</span> <span class="n">buffer</span><span class="p">[</span><span class="mi">1</span><span class="o">&lt;&lt;</span><span class="mi">21</span><span class="p">];</span><span class="kt">int</span> <span class="n">p1</span><span class="o">=</span><span class="mi">-1</span><span class="p">;</span><span class="k">const</span> <span class="kt">int</span> <span class="n">p2</span> <span class="o">=</span> <span class="p">(</span><span class="mi">1</span><span class="o">&lt;&lt;</span><span class="mi">21</span><span class="p">)</span><span class="mi">-1</span><span class="p">;</span>
<span class="linenos" data-linenos=" 13 "></span>    <span class="kr">inline</span> <span class="kt">void</span> <span class="n">flush</span><span class="p">(){</span><span class="n">fwrite</span><span class="p">(</span><span class="n">buffer</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="n">p1</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span><span class="n">stdout</span><span class="p">),</span><span class="n">p1</span><span class="o">=</span><span class="mi">-1</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 14 "></span>    <span class="kr">inline</span> <span class="kt">void</span> <span class="n">putc</span><span class="p">(</span><span class="k">const</span> <span class="kt">char</span> <span class="o">&amp;</span><span class="n">x</span><span class="p">)</span> <span class="p">{</span><span class="k">if</span><span class="p">(</span><span class="n">p1</span><span class="o">==</span><span class="n">p2</span><span class="p">)</span><span class="n">flush</span><span class="p">();</span><span class="n">buffer</span><span class="p">[</span><span class="o">++</span><span class="n">p1</span><span class="p">]</span><span class="o">=</span><span class="n">x</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 15 "></span>    <span class="k">template</span> <span class="o">&lt;</span><span class="k">typename</span> <span class="nc">T</span><span class="o">&gt;</span><span class="kt">void</span> <span class="n">write</span><span class="p">(</span><span class="n">T</span> <span class="n">x</span><span class="p">)</span> <span class="p">{</span>
<span class="linenos" data-linenos=" 16 "></span>        <span class="k">static</span> <span class="kt">char</span> <span class="n">buf</span><span class="p">[</span><span class="mi">15</span><span class="p">];</span><span class="k">static</span> <span class="kt">int</span> <span class="n">len</span><span class="o">=</span><span class="mi">-1</span><span class="p">;</span><span class="k">if</span><span class="p">(</span><span class="n">x</span><span class="o">&gt;=</span><span class="mi">0</span><span class="p">){</span><span class="k">do</span><span class="p">{</span><span class="n">buf</span><span class="p">[</span><span class="o">++</span><span class="n">len</span><span class="p">]</span><span class="o">=</span><span class="n">x</span><span class="o">%</span><span class="mi">10</span><span class="o">+</span><span class="mi">48</span><span class="p">,</span><span class="n">x</span><span class="o">/=</span><span class="mi">10</span><span class="p">;}</span><span class="k">while</span> <span class="p">(</span><span class="n">x</span><span class="p">);}</span><span class="k">else</span><span class="p">{</span><span class="n">putc</span><span class="p">(</span><span class="sc">&#39;-&#39;</span><span class="p">);</span><span class="k">do</span> <span class="p">{</span><span class="n">buf</span><span class="p">[</span><span class="o">++</span><span class="n">len</span><span class="p">]</span><span class="o">=-</span><span class="p">(</span><span class="n">x</span><span class="o">%</span><span class="mi">10</span><span class="p">)</span><span class="o">+</span><span class="mi">48</span><span class="p">,</span><span class="n">x</span><span class="o">/=</span><span class="mi">10</span><span class="p">;}</span><span class="k">while</span><span class="p">(</span><span class="n">x</span><span class="p">);}</span>
<span class="linenos" data-linenos=" 17 "></span>        <span class="k">while</span> <span class="p">(</span><span class="n">len</span><span class="o">&gt;=</span><span class="mi">0</span><span class="p">)</span><span class="n">putc</span><span class="p">(</span><span class="n">buf</span><span class="p">[</span><span class="n">len</span><span class="p">]),</span><span class="o">--</span><span class="n">len</span><span class="p">;</span>
<span class="linenos" data-linenos=" 18 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos=" 19 "></span><span class="p">}</span>
<span class="linenos" data-linenos=" 20 "></span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span>
<span class="linenos" data-linenos=" 21 "></span><span class="kt">int</span> <span class="n">n</span><span class="p">,</span><span class="n">q</span><span class="p">;</span>
<span class="linenos" data-linenos=" 22 "></span><span class="c1">//双向链表部分</span>
<span class="linenos" data-linenos=" 23 "></span><span class="k">struct</span> <span class="nc">node</span><span class="p">{</span>
<span class="linenos" data-linenos=" 24 "></span>    <span class="kt">int</span> <span class="n">pre</span><span class="p">,</span><span class="n">nxt</span><span class="p">;</span><span class="c1">//记录前驱与后继 </span>
<span class="linenos" data-linenos=" 25 "></span>    <span class="n">node</span><span class="p">(){</span><span class="n">pre</span><span class="o">=</span><span class="n">nxt</span><span class="o">=</span><span class="mi">0</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 26 "></span>    <span class="n">node</span><span class="p">(</span><span class="kt">int</span> <span class="n">_pre</span><span class="p">,</span><span class="kt">int</span> <span class="n">_nxt</span><span class="p">){</span><span class="n">pre</span><span class="o">=</span><span class="n">_pre</span><span class="p">,</span><span class="n">nxt</span><span class="o">=</span><span class="n">_nxt</span><span class="p">;};</span>
<span class="linenos" data-linenos=" 27 "></span><span class="p">};</span> 
<span class="linenos" data-linenos=" 28 "></span><span class="n">map</span><span class="o">&lt;</span><span class="kt">int</span><span class="p">,</span><span class="n">node</span><span class="o">&gt;</span><span class="n">List</span><span class="p">;</span>
<span class="linenos" data-linenos=" 29 "></span><span class="kt">bool</span> <span class="nf">has</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">){</span>
<span class="linenos" data-linenos=" 30 "></span>    <span class="k">return</span> <span class="n">List</span><span class="p">.</span><span class="n">find</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">!=</span><span class="n">List</span><span class="p">.</span><span class="n">end</span><span class="p">();</span>
<span class="linenos" data-linenos=" 31 "></span><span class="p">}</span>
<span class="linenos" data-linenos=" 32 "></span><span class="kt">void</span> <span class="nf">chk</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">){</span>
<span class="linenos" data-linenos=" 33 "></span>    <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">has</span><span class="p">(</span><span class="n">x</span><span class="p">))</span>
<span class="linenos" data-linenos=" 34 "></span>        <span class="n">List</span><span class="p">[</span><span class="n">x</span><span class="p">]</span><span class="o">=</span><span class="n">node</span><span class="p">(</span><span class="n">x</span><span class="mi">-1</span><span class="p">,</span><span class="n">x</span><span class="o">+</span><span class="mi">1</span><span class="p">);</span>
<span class="linenos" data-linenos=" 35 "></span><span class="p">}</span>
<span class="linenos" data-linenos=" 36 "></span><span class="kt">void</span> <span class="nf">connect</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">,</span><span class="kt">int</span> <span class="n">y</span><span class="p">){</span>
<span class="linenos" data-linenos=" 37 "></span>    <span class="n">List</span><span class="p">[</span><span class="n">x</span><span class="p">].</span><span class="n">nxt</span><span class="o">=</span><span class="n">y</span><span class="p">;</span>
<span class="linenos" data-linenos=" 38 "></span>    <span class="n">List</span><span class="p">[</span><span class="n">y</span><span class="p">].</span><span class="n">pre</span><span class="o">=</span><span class="n">x</span><span class="p">;</span>
<span class="linenos" data-linenos=" 39 "></span><span class="p">}</span>
<span class="linenos" data-linenos=" 40 "></span><span class="c1">//维护块</span>
<span class="linenos" data-linenos=" 41 "></span><span class="n">vector</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span><span class="n">num</span><span class="p">[</span><span class="mi">100000</span><span class="o">+</span><span class="mi">10</span><span class="p">];</span>
<span class="linenos" data-linenos=" 42 "></span><span class="kt">int</span> <span class="n">cnt</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span>
<span class="linenos" data-linenos=" 43 "></span><span class="k">struct</span> <span class="nc">node2</span><span class="p">{</span>
<span class="linenos" data-linenos=" 44 "></span>    <span class="kt">int</span> <span class="n">pre</span><span class="p">,</span><span class="n">nxt</span><span class="p">,</span><span class="n">id</span><span class="p">,</span><span class="n">sum</span><span class="p">,</span><span class="n">lensum</span><span class="p">;</span>
<span class="linenos" data-linenos=" 45 "></span>    <span class="n">node2</span><span class="p">(){</span><span class="n">id</span><span class="o">=</span><span class="n">cnt</span><span class="o">++</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 46 "></span>    <span class="kt">bool</span> <span class="k">operator</span><span class="o">&lt;</span><span class="p">(</span><span class="k">const</span> <span class="n">node2</span> <span class="n">b</span><span class="p">)</span><span class="k">const</span><span class="p">{</span>
<span class="linenos" data-linenos=" 47 "></span>        <span class="k">return</span> <span class="n">pre</span><span class="o">&lt;</span><span class="n">b</span><span class="p">.</span><span class="n">pre</span><span class="p">;</span>
<span class="linenos" data-linenos=" 48 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos=" 49 "></span><span class="p">};</span>
<span class="linenos" data-linenos=" 50 "></span><span class="n">vector</span><span class="o">&lt;</span><span class="n">node2</span><span class="o">&gt;</span><span class="n">block</span><span class="p">;</span>
<span class="linenos" data-linenos=" 51 "></span><span class="c1">//维护块内有关信息</span>
<span class="linenos" data-linenos=" 52 "></span><span class="n">map</span><span class="o">&lt;</span><span class="kt">int</span><span class="p">,</span><span class="kt">int</span><span class="o">&gt;</span><span class="n">wei</span><span class="p">;</span><span class="c1">//wei[x] 记录x的位置</span>
<span class="linenos" data-linenos=" 53 "></span><span class="n">map</span><span class="o">&lt;</span><span class="kt">int</span><span class="p">,</span><span class="kt">int</span><span class="o">&gt;</span><span class="n">val</span><span class="p">;</span><span class="c1">//val[x] 记录x上的值</span>
<span class="linenos" data-linenos=" 54 "></span><span class="c1">//动态开点线段树查询</span>
<span class="linenos" data-linenos=" 55 "></span><span class="k">struct</span> <span class="nc">t</span><span class="p">{</span>
<span class="linenos" data-linenos=" 56 "></span>    <span class="kt">int</span> <span class="n">lc</span><span class="p">,</span><span class="n">rc</span><span class="p">;</span>
<span class="linenos" data-linenos=" 57 "></span>    <span class="kt">int</span> <span class="n">sz</span><span class="p">;</span>
<span class="linenos" data-linenos=" 58 "></span><span class="p">}</span><span class="n">T</span><span class="p">[</span><span class="mi">100000</span><span class="o">*</span><span class="mi">32</span><span class="p">];</span>
<span class="linenos" data-linenos=" 59 "></span><span class="kt">int</span> <span class="n">root</span><span class="p">,</span><span class="n">tot</span><span class="p">;</span>
<span class="linenos" data-linenos=" 60 "></span><span class="cp">#define mid (l+r&gt;&gt;1)</span>
<span class="linenos" data-linenos=" 61 "></span><span class="kt">void</span> <span class="nf">upd</span><span class="p">(</span><span class="kt">int</span> <span class="o">&amp;</span><span class="n">x</span><span class="p">,</span><span class="kt">int</span> <span class="n">l</span><span class="p">,</span><span class="kt">int</span> <span class="n">r</span><span class="p">,</span><span class="kt">int</span> <span class="n">pos</span><span class="p">){</span>
<span class="linenos" data-linenos=" 62 "></span>    <span class="c1">//把pos改为0</span>
<span class="linenos" data-linenos=" 63 "></span>    <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">x</span><span class="p">)</span><span class="n">x</span><span class="o">=++</span><span class="n">tot</span><span class="p">,</span><span class="n">T</span><span class="p">[</span><span class="n">x</span><span class="p">].</span><span class="n">sz</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span>
<span class="linenos" data-linenos=" 64 "></span>    <span class="k">if</span><span class="p">(</span><span class="n">l</span><span class="o">==</span><span class="n">r</span><span class="p">){</span><span class="n">T</span><span class="p">[</span><span class="n">x</span><span class="p">].</span><span class="n">sz</span><span class="o">=</span><span class="mi">1</span><span class="p">;</span><span class="k">return</span><span class="p">;}</span>
<span class="linenos" data-linenos=" 65 "></span>    <span class="k">if</span><span class="p">(</span><span class="n">pos</span><span class="o">&lt;=</span><span class="n">mid</span><span class="p">)</span><span class="n">upd</span><span class="p">(</span><span class="n">T</span><span class="p">[</span><span class="n">x</span><span class="p">].</span><span class="n">lc</span><span class="p">,</span><span class="n">l</span><span class="p">,</span><span class="n">mid</span><span class="p">,</span><span class="n">pos</span><span class="p">);</span>
<span class="linenos" data-linenos=" 66 "></span>    <span class="k">else</span> <span class="n">upd</span><span class="p">(</span><span class="n">T</span><span class="p">[</span><span class="n">x</span><span class="p">].</span><span class="n">rc</span><span class="p">,</span><span class="n">mid</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span><span class="n">r</span><span class="p">,</span><span class="n">pos</span><span class="p">);</span>
<span class="linenos" data-linenos=" 67 "></span>    <span class="n">T</span><span class="p">[</span><span class="n">x</span><span class="p">].</span><span class="n">sz</span><span class="o">=</span><span class="n">T</span><span class="p">[</span><span class="n">T</span><span class="p">[</span><span class="n">x</span><span class="p">].</span><span class="n">lc</span><span class="p">].</span><span class="n">sz</span><span class="o">+</span><span class="n">T</span><span class="p">[</span><span class="n">T</span><span class="p">[</span><span class="n">x</span><span class="p">].</span><span class="n">rc</span><span class="p">].</span><span class="n">sz</span><span class="p">;</span>
<span class="linenos" data-linenos=" 68 "></span>    <span class="c1">//printf(&quot;%d [%d,%d] %d\n&quot;,x,l,r,t[x].sz); </span>
<span class="linenos" data-linenos=" 69 "></span><span class="p">}</span>
<span class="linenos" data-linenos=" 70 "></span><span class="kt">int</span> <span class="nf">qry1</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">,</span><span class="kt">int</span> <span class="n">l</span><span class="p">,</span><span class="kt">int</span> <span class="n">r</span><span class="p">,</span><span class="kt">int</span> <span class="n">pos</span><span class="p">){</span>
<span class="linenos" data-linenos=" 71 "></span>    <span class="c1">//printf(&quot;%d %d %d   sz:%d\n&quot;,x,l,r,T[x].sz);</span>
<span class="linenos" data-linenos=" 72 "></span>    <span class="c1">//查询在小于等于pos的和</span>
<span class="linenos" data-linenos=" 73 "></span>    <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">x</span><span class="p">)</span><span class="k">return</span> <span class="n">pos</span><span class="o">-</span><span class="n">l</span><span class="o">+</span><span class="mi">1</span><span class="p">;</span>
<span class="linenos" data-linenos=" 74 "></span>    <span class="k">if</span><span class="p">(</span><span class="n">l</span><span class="o">==</span><span class="n">r</span><span class="p">)</span><span class="k">return</span> <span class="mi">1</span><span class="o">-</span><span class="n">T</span><span class="p">[</span><span class="n">x</span><span class="p">].</span><span class="n">sz</span><span class="p">;</span>
<span class="linenos" data-linenos=" 75 "></span>    <span class="k">if</span><span class="p">(</span><span class="n">pos</span><span class="o">&lt;=</span><span class="n">mid</span><span class="p">)</span><span class="k">return</span> <span class="n">qry1</span><span class="p">(</span><span class="n">T</span><span class="p">[</span><span class="n">x</span><span class="p">].</span><span class="n">lc</span><span class="p">,</span><span class="n">l</span><span class="p">,</span><span class="n">mid</span><span class="p">,</span><span class="n">pos</span><span class="p">);</span>
<span class="linenos" data-linenos=" 76 "></span>    <span class="k">return</span> <span class="p">(</span><span class="n">mid</span><span class="o">-</span><span class="n">l</span><span class="o">+</span><span class="mi">1</span><span class="o">-</span><span class="n">T</span><span class="p">[</span><span class="n">T</span><span class="p">[</span><span class="n">x</span><span class="p">].</span><span class="n">lc</span><span class="p">].</span><span class="n">sz</span><span class="p">)</span><span class="o">+</span><span class="n">qry1</span><span class="p">(</span><span class="n">T</span><span class="p">[</span><span class="n">x</span><span class="p">].</span><span class="n">rc</span><span class="p">,</span><span class="n">mid</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span><span class="n">r</span><span class="p">,</span><span class="n">pos</span><span class="p">);</span>
<span class="linenos" data-linenos=" 77 "></span><span class="p">}</span>
<span class="linenos" data-linenos=" 78 "></span><span class="kt">int</span> <span class="nf">qry2</span><span class="p">(</span><span class="kt">int</span> <span class="n">x</span><span class="p">,</span><span class="kt">int</span> <span class="n">l</span><span class="p">,</span><span class="kt">int</span> <span class="n">r</span><span class="p">,</span><span class="kt">int</span> <span class="n">pos</span><span class="p">){</span>
<span class="linenos" data-linenos=" 79 "></span>    <span class="c1">//查询第 pos 个未被删除的值</span>
<span class="linenos" data-linenos=" 80 "></span>    <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">x</span><span class="p">){</span>
<span class="linenos" data-linenos=" 81 "></span>        <span class="c1">//说明l-r都未被删除</span>
<span class="linenos" data-linenos=" 82 "></span>        <span class="k">return</span> <span class="n">l</span><span class="o">+</span><span class="n">pos</span><span class="mi">-1</span><span class="p">;</span> 
<span class="linenos" data-linenos=" 83 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos=" 84 "></span>    <span class="kt">int</span> <span class="n">hasl</span><span class="o">=</span><span class="n">mid</span><span class="o">-</span><span class="n">l</span><span class="o">+</span><span class="mi">1</span><span class="o">-</span><span class="n">T</span><span class="p">[</span><span class="n">T</span><span class="p">[</span><span class="n">x</span><span class="p">].</span><span class="n">lc</span><span class="p">].</span><span class="n">sz</span><span class="p">;</span>
<span class="linenos" data-linenos=" 85 "></span>    <span class="k">if</span><span class="p">(</span><span class="n">hasl</span><span class="o">&gt;=</span><span class="n">pos</span><span class="p">)</span><span class="k">return</span> <span class="n">qry2</span><span class="p">(</span><span class="n">T</span><span class="p">[</span><span class="n">x</span><span class="p">].</span><span class="n">lc</span><span class="p">,</span><span class="n">l</span><span class="p">,</span><span class="n">mid</span><span class="p">,</span><span class="n">pos</span><span class="p">);</span>
<span class="linenos" data-linenos=" 86 "></span>    <span class="k">else</span> <span class="k">return</span> <span class="n">qry2</span><span class="p">(</span><span class="n">T</span><span class="p">[</span><span class="n">x</span><span class="p">].</span><span class="n">rc</span><span class="p">,</span><span class="n">mid</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span><span class="n">r</span><span class="p">,</span><span class="n">pos</span><span class="o">-</span><span class="n">hasl</span><span class="p">);</span>
<span class="linenos" data-linenos=" 87 "></span><span class="p">}</span>
<span class="linenos" data-linenos=" 88 "></span><span class="kt">signed</span> <span class="nf">main</span><span class="p">(){</span>
<span class="linenos" data-linenos=" 89 "></span>    <span class="c1">//freopen(&quot;1.in&quot;,&quot;r&quot;,stdin);</span>
<span class="linenos" data-linenos=" 90 "></span>    <span class="c1">//freopen(&quot;1.out&quot;,&quot;w&quot;,stdout);</span>
<span class="linenos" data-linenos=" 91 "></span>    <span class="n">in</span><span class="o">::</span><span class="n">read</span><span class="p">(</span><span class="n">n</span><span class="p">);</span>
<span class="linenos" data-linenos=" 92 "></span>    <span class="k">for</span><span class="p">(</span><span class="kt">int</span> <span class="n">i</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span><span class="n">a</span><span class="p">,</span><span class="n">b</span><span class="p">;</span><span class="n">i</span><span class="o">&lt;=</span><span class="n">n</span><span class="p">;</span><span class="n">i</span><span class="o">++</span><span class="p">){</span>
<span class="linenos" data-linenos=" 93 "></span>        <span class="n">in</span><span class="o">::</span><span class="n">read</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">b</span><span class="p">);</span>
<span class="linenos" data-linenos=" 94 "></span>        <span class="n">chk</span><span class="p">(</span><span class="n">a</span><span class="p">);</span><span class="n">chk</span><span class="p">(</span><span class="n">List</span><span class="p">[</span><span class="n">a</span><span class="p">].</span><span class="n">pre</span><span class="p">);</span><span class="n">chk</span><span class="p">(</span><span class="n">List</span><span class="p">[</span><span class="n">a</span><span class="p">].</span><span class="n">nxt</span><span class="p">);</span>
<span class="linenos" data-linenos=" 95 "></span>        <span class="n">chk</span><span class="p">(</span><span class="n">b</span><span class="p">);</span><span class="n">chk</span><span class="p">(</span><span class="n">List</span><span class="p">[</span><span class="n">b</span><span class="p">].</span><span class="n">pre</span><span class="p">);</span>
<span class="linenos" data-linenos=" 96 "></span>        <span class="n">connect</span><span class="p">(</span><span class="n">List</span><span class="p">[</span><span class="n">a</span><span class="p">].</span><span class="n">pre</span><span class="p">,</span><span class="n">List</span><span class="p">[</span><span class="n">a</span><span class="p">].</span><span class="n">nxt</span><span class="p">);</span>
<span class="linenos" data-linenos=" 97 "></span>        <span class="n">connect</span><span class="p">(</span><span class="n">List</span><span class="p">[</span><span class="n">b</span><span class="p">].</span><span class="n">pre</span><span class="p">,</span><span class="n">a</span><span class="p">);</span>
<span class="linenos" data-linenos=" 98 "></span>        <span class="n">connect</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">b</span><span class="p">);</span>
<span class="linenos" data-linenos=" 99 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="100 "></span>    <span class="n">List</span><span class="p">.</span><span class="n">erase</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>
<span class="linenos" data-linenos="101 "></span>    <span class="k">for</span><span class="p">(</span><span class="k">auto</span> <span class="nl">k</span><span class="p">:</span><span class="n">List</span><span class="p">){</span>
<span class="linenos" data-linenos="102 "></span>        <span class="c1">//printf(&quot;%d&lt;- %d -&gt;%d\n&quot;,k.second.pre,k.first,k.second.nxt); </span>
<span class="linenos" data-linenos="103 "></span>        <span class="k">if</span><span class="p">(</span><span class="o">!</span><span class="n">has</span><span class="p">(</span><span class="n">k</span><span class="p">.</span><span class="n">second</span><span class="p">.</span><span class="n">pre</span><span class="p">)){</span>
<span class="linenos" data-linenos="104 "></span>            <span class="n">block</span><span class="p">.</span><span class="n">push_back</span><span class="p">(</span><span class="n">node2</span><span class="p">());</span>
<span class="linenos" data-linenos="105 "></span>            <span class="n">block</span><span class="p">.</span><span class="n">back</span><span class="p">().</span><span class="n">pre</span><span class="o">=</span><span class="n">k</span><span class="p">.</span><span class="n">second</span><span class="p">.</span><span class="n">pre</span><span class="p">;</span>
<span class="linenos" data-linenos="106 "></span>            <span class="kt">int</span> <span class="n">now</span><span class="o">=</span><span class="n">k</span><span class="p">.</span><span class="n">first</span><span class="p">;</span>
<span class="linenos" data-linenos="107 "></span>            <span class="k">while</span><span class="p">(</span><span class="n">has</span><span class="p">(</span><span class="n">now</span><span class="p">)){</span>
<span class="linenos" data-linenos="108 "></span>                <span class="n">num</span><span class="p">[</span><span class="n">block</span><span class="p">.</span><span class="n">back</span><span class="p">().</span><span class="n">id</span><span class="p">].</span><span class="n">push_back</span><span class="p">(</span><span class="n">now</span><span class="p">);</span>
<span class="linenos" data-linenos="109 "></span>                <span class="n">now</span><span class="o">=</span><span class="n">List</span><span class="p">[</span><span class="n">now</span><span class="p">].</span><span class="n">nxt</span><span class="p">;</span>
<span class="linenos" data-linenos="110 "></span>            <span class="p">}</span>
<span class="linenos" data-linenos="111 "></span>            <span class="n">block</span><span class="p">.</span><span class="n">back</span><span class="p">().</span><span class="n">nxt</span><span class="o">=</span><span class="n">now</span><span class="p">;</span>
<span class="linenos" data-linenos="112 "></span>        <span class="p">}</span>
<span class="linenos" data-linenos="113 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="114 "></span>    <span class="n">block</span><span class="p">.</span><span class="n">push_back</span><span class="p">(</span><span class="n">node2</span><span class="p">());</span>
<span class="linenos" data-linenos="115 "></span>    <span class="n">block</span><span class="p">.</span><span class="n">back</span><span class="p">().</span><span class="n">pre</span><span class="o">=</span><span class="mi">-1000</span><span class="p">;</span>
<span class="linenos" data-linenos="116 "></span>    <span class="n">block</span><span class="p">.</span><span class="n">back</span><span class="p">().</span><span class="n">sum</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span>
<span class="linenos" data-linenos="117 "></span>    <span class="n">block</span><span class="p">.</span><span class="n">back</span><span class="p">().</span><span class="n">lensum</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span>
<span class="linenos" data-linenos="118 "></span>
<span class="linenos" data-linenos="119 "></span>    <span class="n">sort</span><span class="p">(</span><span class="n">block</span><span class="p">.</span><span class="n">begin</span><span class="p">(),</span><span class="n">block</span><span class="p">.</span><span class="n">end</span><span class="p">());</span>
<span class="linenos" data-linenos="120 "></span>    <span class="c1">//for(auto k:block){</span>
<span class="linenos" data-linenos="121 "></span>    <span class="c1">//  printf(&quot;%d %d\n&quot;,k.pre,k.nxt);</span>
<span class="linenos" data-linenos="122 "></span>    <span class="c1">//  for(auto nn:num[k.id])</span>
<span class="linenos" data-linenos="123 "></span>    <span class="c1">//      printf(&quot;%d &quot;,nn);</span>
<span class="linenos" data-linenos="124 "></span>    <span class="c1">//  printf(&quot;\n&quot;);</span>
<span class="linenos" data-linenos="125 "></span>    <span class="c1">//}</span>
<span class="linenos" data-linenos="126 "></span>
<span class="linenos" data-linenos="127 "></span>    <span class="kt">int</span> <span class="n">lst</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span><span class="n">sum</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span><span class="n">lensum</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span><span class="c1">//记录上一个块的后继 ，已经一共有的数</span>
<span class="linenos" data-linenos="128 "></span>    <span class="k">for</span><span class="p">(</span><span class="k">auto</span> <span class="o">&amp;</span><span class="nl">k</span><span class="p">:</span><span class="n">block</span><span class="p">){</span>
<span class="linenos" data-linenos="129 "></span>        <span class="k">if</span><span class="p">(</span><span class="n">k</span><span class="p">.</span><span class="n">pre</span><span class="o">&lt;</span><span class="mi">0</span><span class="p">)</span><span class="k">continue</span><span class="p">;</span>
<span class="linenos" data-linenos="130 "></span>        <span class="n">sum</span><span class="o">+=</span><span class="n">k</span><span class="p">.</span><span class="n">pre</span><span class="o">-</span><span class="n">lst</span><span class="o">+</span><span class="mi">1</span><span class="p">;</span>
<span class="linenos" data-linenos="131 "></span>        <span class="n">lst</span><span class="o">=</span><span class="n">k</span><span class="p">.</span><span class="n">nxt</span><span class="p">;</span>
<span class="linenos" data-linenos="132 "></span>        <span class="k">for</span><span class="p">(</span><span class="k">auto</span> <span class="nl">nn</span><span class="p">:</span><span class="n">num</span><span class="p">[</span><span class="n">k</span><span class="p">.</span><span class="n">id</span><span class="p">])</span>
<span class="linenos" data-linenos="133 "></span>            <span class="n">sum</span><span class="o">++</span><span class="p">,</span><span class="n">wei</span><span class="p">[</span><span class="n">nn</span><span class="p">]</span><span class="o">=</span><span class="n">sum</span><span class="p">,</span><span class="n">val</span><span class="p">[</span><span class="n">sum</span><span class="p">]</span><span class="o">=</span><span class="n">nn</span><span class="p">,</span>
<span class="linenos" data-linenos="134 "></span>            <span class="n">upd</span><span class="p">(</span><span class="n">root</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1000000000</span><span class="p">,</span><span class="n">nn</span><span class="p">);</span>
<span class="linenos" data-linenos="135 "></span>        <span class="n">k</span><span class="p">.</span><span class="n">sum</span><span class="o">=</span><span class="n">sum</span><span class="p">;</span>
<span class="linenos" data-linenos="136 "></span>        <span class="n">k</span><span class="p">.</span><span class="n">lensum</span><span class="o">=</span><span class="n">lensum</span><span class="o">+</span><span class="n">num</span><span class="p">[</span><span class="n">k</span><span class="p">.</span><span class="n">id</span><span class="p">].</span><span class="n">size</span><span class="p">();</span>
<span class="linenos" data-linenos="137 "></span>        <span class="n">lensum</span><span class="o">=</span><span class="n">k</span><span class="p">.</span><span class="n">lensum</span><span class="p">;</span>
<span class="linenos" data-linenos="138 "></span>    <span class="p">}</span> 
<span class="linenos" data-linenos="139 "></span>    <span class="c1">//for(auto k:wei)</span>
<span class="linenos" data-linenos="140 "></span>    <span class="c1">//  printf(&quot;%d在%d\n&quot;,k.first,k.second);</span>
<span class="linenos" data-linenos="141 "></span>    <span class="n">in</span><span class="o">::</span><span class="n">read</span><span class="p">(</span><span class="n">q</span><span class="p">);</span>
<span class="linenos" data-linenos="142 "></span>    <span class="k">while</span><span class="p">(</span><span class="n">q</span><span class="o">--</span><span class="p">){</span>
<span class="linenos" data-linenos="143 "></span>        <span class="kt">char</span> <span class="n">op</span><span class="o">=</span><span class="n">in</span><span class="o">::</span><span class="n">getc</span><span class="p">();</span><span class="kt">int</span> <span class="n">x</span><span class="p">;</span>
<span class="linenos" data-linenos="144 "></span>        <span class="k">while</span><span class="p">(</span><span class="o">!</span><span class="n">isalpha</span><span class="p">(</span><span class="n">op</span><span class="p">))</span><span class="n">op</span><span class="o">=</span><span class="n">in</span><span class="o">::</span><span class="n">getc</span><span class="p">();</span>
<span class="linenos" data-linenos="145 "></span>        <span class="n">in</span><span class="o">::</span><span class="n">read</span><span class="p">(</span><span class="n">x</span><span class="p">);</span>
<span class="linenos" data-linenos="146 "></span>        <span class="k">if</span><span class="p">(</span><span class="n">op</span><span class="o">==</span><span class="sc">&#39;P&#39;</span><span class="p">){</span>
<span class="linenos" data-linenos="147 "></span>            <span class="k">if</span><span class="p">(</span><span class="n">wei</span><span class="p">[</span><span class="n">x</span><span class="p">])</span>
<span class="linenos" data-linenos="148 "></span>                <span class="n">out</span><span class="o">::</span><span class="n">write</span><span class="p">(</span><span class="n">wei</span><span class="p">[</span><span class="n">x</span><span class="p">]);</span>
<span class="linenos" data-linenos="149 "></span>            <span class="k">else</span><span class="p">{</span>
<span class="linenos" data-linenos="150 "></span>                <span class="kt">int</span> <span class="n">l</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span><span class="n">r</span><span class="o">=</span><span class="n">block</span><span class="p">.</span><span class="n">size</span><span class="p">()</span><span class="mi">-1</span><span class="p">;</span>
<span class="linenos" data-linenos="151 "></span>                <span class="kt">int</span> <span class="n">ans</span><span class="o">=</span><span class="mi">0</span><span class="p">;</span>
<span class="linenos" data-linenos="152 "></span>                <span class="k">while</span><span class="p">(</span><span class="n">l</span><span class="o">&lt;=</span><span class="n">r</span><span class="p">){</span>
<span class="linenos" data-linenos="153 "></span>                    <span class="k">if</span><span class="p">(</span><span class="n">block</span><span class="p">[</span><span class="n">mid</span><span class="p">].</span><span class="n">nxt</span><span class="o">&lt;=</span><span class="n">x</span><span class="p">)</span>
<span class="linenos" data-linenos="154 "></span>                        <span class="n">ans</span><span class="o">=</span><span class="n">mid</span><span class="p">,</span><span class="n">l</span><span class="o">=</span><span class="n">mid</span><span class="o">+</span><span class="mi">1</span><span class="p">;</span>
<span class="linenos" data-linenos="155 "></span>                    <span class="k">else</span> <span class="n">r</span><span class="o">=</span><span class="n">mid</span><span class="mi">-1</span><span class="p">;</span>
<span class="linenos" data-linenos="156 "></span>                <span class="p">}</span>
<span class="linenos" data-linenos="157 "></span>                <span class="n">out</span><span class="o">::</span><span class="n">write</span><span class="p">(</span><span class="n">block</span><span class="p">[</span><span class="n">ans</span><span class="p">].</span><span class="n">lensum</span><span class="o">+</span><span class="n">qry1</span><span class="p">(</span><span class="n">root</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1000000000</span><span class="p">,</span><span class="n">x</span><span class="p">));</span>
<span class="linenos" data-linenos="158 "></span>            <span class="p">}</span>
<span class="linenos" data-linenos="159 "></span>        <span class="p">}</span><span class="k">else</span><span class="p">{</span>
<span class="linenos" data-linenos="160 "></span>            <span class="k">if</span><span class="p">(</span><span class="n">val</span><span class="p">[</span><span class="n">x</span><span class="p">])</span>
<span class="linenos" data-linenos="161 "></span>                <span class="n">out</span><span class="o">::</span><span class="n">write</span><span class="p">(</span><span class="n">val</span><span class="p">[</span><span class="n">x</span><span class="p">]);</span>
<span class="linenos" data-linenos="162 "></span>            <span class="k">else</span><span class="p">{</span>
<span class="linenos" data-linenos="163 "></span>                <span class="kt">int</span> <span class="n">l</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span><span class="n">r</span><span class="o">=</span><span class="n">block</span><span class="p">.</span><span class="n">size</span><span class="p">()</span><span class="mi">-1</span><span class="p">;</span>
<span class="linenos" data-linenos="164 "></span>                <span class="kt">int</span> <span class="n">ans</span><span class="p">;</span> 
<span class="linenos" data-linenos="165 "></span>                <span class="k">while</span><span class="p">(</span><span class="n">l</span><span class="o">&lt;=</span><span class="n">r</span><span class="p">){</span>
<span class="linenos" data-linenos="166 "></span>                    <span class="k">if</span><span class="p">(</span><span class="n">block</span><span class="p">[</span><span class="n">mid</span><span class="p">].</span><span class="n">sum</span><span class="o">&lt;=</span><span class="n">x</span><span class="p">)</span>
<span class="linenos" data-linenos="167 "></span>                        <span class="n">ans</span><span class="o">=</span><span class="n">mid</span><span class="p">,</span><span class="n">l</span><span class="o">=</span><span class="n">mid</span><span class="o">+</span><span class="mi">1</span><span class="p">;</span>
<span class="linenos" data-linenos="168 "></span>                    <span class="k">else</span> <span class="n">r</span><span class="o">=</span><span class="n">mid</span><span class="mi">-1</span><span class="p">;</span>
<span class="linenos" data-linenos="169 "></span>                <span class="p">}</span>
<span class="linenos" data-linenos="170 "></span>                <span class="n">out</span><span class="o">::</span><span class="n">write</span><span class="p">(</span><span class="n">qry2</span><span class="p">(</span><span class="n">root</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">1000000000</span><span class="p">,</span><span class="n">x</span><span class="o">-</span><span class="n">block</span><span class="p">[</span><span class="n">ans</span><span class="p">].</span><span class="n">lensum</span><span class="p">));</span>
<span class="linenos" data-linenos="171 "></span>            <span class="p">}</span>
<span class="linenos" data-linenos="172 "></span>        <span class="p">}</span>
<span class="linenos" data-linenos="173 "></span>        <span class="n">out</span><span class="o">::</span><span class="n">putc</span><span class="p">(</span><span class="sc">&#39;\n&#39;</span><span class="p">);</span>
<span class="linenos" data-linenos="174 "></span>
<span class="linenos" data-linenos="175 "></span>    <span class="p">}</span>
<span class="linenos" data-linenos="176 "></span>    <span class="n">out</span><span class="o">::</span><span class="n">flush</span><span class="p">();</span>
<span class="linenos" data-linenos="177 "></span>    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="linenos" data-linenos="178 "></span><span class="p">}</span>
</code></pre></div></p>
			</div>
			<div class="panel panel-default card" style="padding: 10px;height: 50px;font-size: 20px;">
				<a style="float: left;" href="\article\CF575I.html"><i class="fa fa-angle-double-left"></i></a><a style="float: right;" href="\article\CF101D.html"><i class="fa fa-angle-double-right"></i></a>
			</div>
			<div class="panel panel-default card" style="padding: 10px;font-size: 20px;" id="vcomments">
				
			</div>
			<script>
				new Valine({
					el: '#vcomments',
					appId: 'PAlFUPg0pQVTFTEo8gCB4BCf-gzGzoHsz',
					appKey: 'U38ejnLUiizJ6vLyp6ql4hRq',
					path: window.location.pathname
				})
			</script>
		</div>
		<div class="col-md-3">
				<div class="panel panel-default card">
				<div class="panel-heading">
					<h3 class="panel-title">
						<i class="fa fa-info"></i>&emsp;
						<strong>文章信息</strong>
					</h3>
				</div>
                <div style="margin: 10px;">
                    <div style="margin-top: 8px;display: flex;"> 
    <span style="flex: 1 0 auto;margin-right: 6px;">标题</span>
    <span><font style="font-weight: bold">题解 P6887 【[CEOI2006]Queue】</font></span>
	</div><div style="margin-top: 8px;display: flex;"> 
    <span style="flex: 1 0 auto;margin-right: 6px;">日期</span>
    <span>2020-11-13 18:18:00</span>
	</div>
                </div>
				</div>
				<div class="panel panel-default card">
				<div class="panel-heading">
					<h3 class="panel-title">
						<i class="fa fa-tag"></i>&emsp;
						<strong>标签</strong>
					</h3>
				</div>
				<div style="margin: 10px;">
					<a href="/tags/2020/" class="tag"><span  style="background-color: rgb(52, 152, 219);">2020</span></a>&nbsp;<a href="/tags/线段树/" class="tag"><span  style="background-color: rgb(231, 76, 60);">线段树</span></a>&nbsp;<a href="/tags/链表/" class="tag"><span  style="background-color: rgb(231, 76, 60);">链表</span></a>&nbsp;<a href="/tags/题解/" class="tag"><span  style="background-color: rgb(82, 196, 26);">题解</span></a>
				</div>
				</div>
				
			<div class="panel panel-default card">
					<div class="panel-heading">
						<h3 class="panel-title">
							<i class="fa fa-user-circle-o"></i>&emsp;
							<strong>caijicjl</strong>
						</h3>
					</div>
					<div style="width: 60%;display: inline-block;padding-top: 10px;">
						<ul class="propertyLinks" style="list-style-type: none;">
							<li>
								<img style="vertical-align:middle;position:relative;top:-2px" src="/img/rating-16x16.png">
								Rating:&nbsp;
								<span style="font-weight:bold;color: gray ">312</span>
							</li>
							<li>
								<img style="vertical-align:middle;position:relative;top:-2px" src="/img/star_blue_16.png">
								Contribution:&nbsp;
								<span style="color:gray;font-weight:bold;">0</span>
							</li>
						</ul>
						<ul class="nav-links">
							<li><a href="/settings/">Settings</a></li>
							<li><a href="/archives/">Blog</a></li>
							<li><a href="/teams">Teams</a></li>
							<li><a href="/submissions/dingdingsb">Submissions</a></li>
							<li><a href="/usertalk">Talks</a></li>
							<li><a href="/contests/with/dingdingsb">Contests</a></li>
						</ul>
					</div>
					<div style="display:inline-block;vertical-align: top;margin: 10px;text-align: center;">
						<div style="height:50px;width:50px"><img src="/img/avatar.png"/></div>
						<div><a href="https://www.luogu.com.cn/user/174304" style="font-weight:bold;color:gray">caijicjl</a></div>
					</div>
				</div>
		<div class="panel panel-default card">
				<div class="panel-heading">
					<h3 class="panel-title">
						<i class="fa fa-external-link"></i>&emsp;
						<strong>画中画</strong>
					</h3>
				</div>
				<div style="width:100%;margin:10px">
				<input type="text" id="url"/>
				<input value="创建" type="button" onclick="creat('kk')" /> 
				</div>
		</div>
		<script src="/js/hitokoto.js"></script>
							

				
				<span id="kk"></span>
				<div  id="myScrollspy" class="card" style="width: 100%;"  data-spy="affix">
					<script src="/js/make_toc.js"></script>
				</div>
		</div>
	</div>
 </div>
</body>
</html>
